package com.imessage.action.manage.user;

import com.bean.ManageRoleUser;
import com.bean.ManageUser;
import com.imessage.comm.Constants;
import com.imessage.type.BaseState;
import com.imessage.utils.MD5Encrypt;
import com.imessage.vo.UserVo;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/user")
public class UserAction extends UserBaseAction{


    /**
     * 列表数据,搜索数据
     */
    @RequestMapping(value = "")
    public Object run(UserVo userVo) {
        List<Expression> expressionList = new ArrayList<Expression>();
        setExpressionList(expressionList, userVo);
        Page<ManageUser> page = baseService.findPage(ManageUser.class, expressionList,
                null, " id desc ", userVo.getPageCurrent(),
                userVo.getPageSize());
        return getUserBeanPage(page);
    }


    /**
     * 添加修改界面
     */
    @RequestMapping(value = "/initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        ManageUser manageUser = new ManageUser();
        ManageRoleUser manageUserRole = new ManageRoleUser();
        if (StringUtils.isNotBlank(id)) {
            manageUser = baseService.findById(ManageUser.class, id);
            manageUserRole = getManageUserRole(manageUser.getId());
            modelAndView.setViewName("admin/manage/user/modify");
        } else {
            modelAndView.setViewName("admin/manage/user/add");
        }
        setModelAndView(modelAndView, manageUser, manageUserRole);
        return modelAndView;
    }



    /**
     * 添加管理用户
     */
    @RequestMapping(value = "/add")
    public Object add(UserVo userVo) {
        if (existManageUser(userVo.getName(), null)) {
            return getJsonForErr("登录名已经存在", datagrid);
        }
        ManageUser manageUser = new ManageUser();
        setManageUser(userVo, manageUser);
        int state = baseService.saveObject(manageUser);
        saveManageUserRole(userVo.getRoleid(), manageUser.getId());
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 修改管理用户
     */
    @RequestMapping(value = "/modify")
    public Object modify(UserVo userVo) {
        ManageUser manageUser = baseService.findById(ManageUser.class, userVo.getId());
        String username = manageUser.getName();
        setManageUser(userVo, manageUser);
        manageUser.setName(username);
        int state = baseService.updateObject(manageUser);
        List<ManageRoleUser> manageUserRoleList = getManageUserRoleList(userVo.getId());
        if (manageUserRoleList != null & manageUserRoleList.size() > 0) {
            for (ManageRoleUser manageUserRole : manageUserRoleList) {
                baseService.delObject(manageUserRole);
            }
        }
        saveManageUserRole(userVo.getRoleid(), manageUser.getId());
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getUserBean(manageUser);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 删除管理用户
     */
    @RequestMapping(value = "/del")
    public Object del(
            @RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            ManageUser manageUser = baseService.findById(ManageUser.class, id);
            manageUser.setState(BaseState.delete);
            state = baseService.updateObject(manageUser);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }



    /**
     * 重置密码
     */
    @RequestMapping(value = "/resetpasswd")
    public Object resetpasswd(UserVo userVo) {
        Map<String, Object> map = new HashMap<>();
        int statusCode = 300;
        String message = "默认记录不可重置";
        if (1 != userVo.getId()) {
            String passwd = "123456";
            ManageUser manageUser = baseService.findById(ManageUser.class, userVo.getId());
            manageUser.setPasswd(MD5Encrypt.MD5(passwd));
            int state = baseService.updateObject(manageUser);
            if (state == Constants.STATE_OPERATOR_SUCC) {
                statusCode = 200;
                message = "初始化密码为" + passwd;
            } else {
                message = "重置密码失败";
            }
        }
        map.put("statusCode", statusCode);
        map.put("message", message);
        return map;
    }

    /**
     * 登录用户自己修改自己密码页面
     */
    @RequestMapping(value = "/passwdview")
    public ModelAndView passwdview() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/manage/user/passwd");
        return modelAndView;
    }

    /**
     * 登录用户自己修改自己密码
     */
    @RequestMapping(value = "/passwd")
    public Object passwd(String oldpasswd, String passwd1, String passwd2) {
        Map<String, Object> map = new HashMap<>();
        int statusCode = 300;
        String message = "操作失败";
        if (StringUtils.isNotBlank(oldpasswd) && StringUtils.isNotBlank(passwd1) && StringUtils.isNotBlank(passwd2)
                && passwd1.equals(passwd2)) {
            ManageUser manageUser = baseService.findById(ManageUser.class, getAdminSession(false).getManageUser().getId());
            if (manageUser != null && MD5Encrypt.MD5(oldpasswd).equals(manageUser.getPasswd())) {
                manageUser.setPasswd(MD5Encrypt.MD5(passwd1));
                int state = baseService.updateObject(manageUser);
                if (state == Constants.STATE_OPERATOR_SUCC) {
                    statusCode = 200;
                    message = "操作成功,新密码为" + passwd1;
                    map.put("closeCurrent", "true");
                }
            }else{
                message="原密码错误";
            }
        }
        map.put("statusCode", statusCode);
        map.put("message", message);
        return map;
    }
}
